<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Silence
 * Date: 3/8/12
 * Time: 3:51 AM
 * To change this template use File | Settings | File Templates.
 */

require_once('api.php');
require_once('constants.php');
require_once('event.php');

$con = db_prepare();

switch ($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        if (isset($_GET['item']) && $_GET['item'] == "folder") {
            $data = new stdClass();
            $data->id = $_GET['id'];
            $event = new Event($data);
            $event_data = $event->read(array('id', 'name', 'leaf', 'status'), FALSE, FALSE);
            print(json_encode(array(
                'data' => isset($event_data['children']) ? $event_data['children'] : array(),
                'success' => true,
            )));
        }
        if (isset($_GET['item']) && $_GET['item'] == "events") {
            $data = new stdClass();
            $data->id = $_GET['node'];
            $event = new Event($data);
            $event_data = $event->read(array('id', 'name', 'leaf', 'status', 'start_date', 'end_date'), TRUE, TRUE);
            print(json_encode(array(
                'data' => isset($event_data['children']) ? $event_data['children'] : array(),
                'success' => true,
            )));
        }
        break;
    case 'POST':
        //add new
        $response['success'] = TRUE;
        if (isset($_GET['item']) && $_GET['item'] == "folder") {
            $param = get_params_from_raw_http_request();
            $data = $param->data;
            unset($data->id);
            $data->name = $data->text;
            $event = new Event($data);
            $response['success'] = TRUE;
            $response['data'] = $event->load(array("id", "name"));
            print json_encode($response);
        }
        if (isset($_GET['item']) && $_GET['item'] == "events") {
            $param = get_params_from_raw_http_request();
            $raw_data = $param->data;
            $raw_data = $raw_data[0];
            //            var_dump($raw_data);
            $data = new stdClass();
            $data->name = $raw_data->Name;
            $data->start_date = $raw_data->StartDate;
            $data->end_date = $raw_data->EndDate;
            $data->parentId = $raw_data->parentId;
            $data->leaf = 1;
            $event = new Event($data);
            $response['data'] = $event->load(array("id", "name", "start_date", "end_date", "leaf"));
            print json_encode($response);
        }
        break;
    case 'PUT':
        // update
        if (isset($_GET['item']) && $_GET['item'] == "folder") {
            $param = get_params_from_raw_http_request();
            $data = $param->data;
            $data->name = $data->text;

            $event = new Event($data);
            if ($data->status >= 0) {
                $event->update();
                $response = array(
                    'success' => TRUE,
                    'data' => $event->read(array('name', 'status', 'leaf', 'parentId'), FALSE, FALSE)
                );
            } else {
                $event->delete();
                $response = array(
                    'success' => TRUE
                );
            }

            print json_encode($response);
        }
        if (isset($_GET['item']) && $_GET['item'] == "events") {
            $param = get_params_from_raw_http_request();
            $raw_data = $param->data;
            $raw_data = $raw_data[0];
            //            var_dump($raw_data);
            $data = new stdClass();
            if (isset($raw_data->Id))
                $data->id = $raw_data->Id;
            if (isset($raw_data->Name))
                $data->name = $raw_data->Name;
            if (isset($raw_data->StartDate))
                $data->start_date = $raw_data->StartDate;
            if (isset($raw_data->EndDate))
                $data->end_date = $raw_data->EndDate;
            if (isset($raw_data->parentId))
                $data->parentId = $raw_data->parentId;
            if (isset($raw_data->leaf))
                $data->leaf = (int)$raw_data->leaf;

            $event = new Event($data);

            $event->update();
            $data = $event->load(array("id", "name", "start_date", "end_date", "leaf"), TRUE, TRUE);
            $response['success'] = true;
            $response['data'] = isset($data['children']) ? $data['children'] : array();
            //            $response['data']['expanded']
            print json_encode($response);
        }

        break;
    case 'DELETE':
        if (isset($_GET['item']) && $_GET['item'] == "events") {
            $param = get_params_from_raw_http_request();
            $raw_data = $param->data;
            $raw_data = $raw_data[0];
            //            var_dump($raw_data);
            $data = new stdClass();
            $data->id = $raw_data->Id;

            $event = new Event($data);
            $d = $event->load(array("leaf"));
            if ($d['leaf']) {
                $event->delete();
            } else {
                $data->status = 0;
                $event->update($data);
            }
            $response['success'] = true;
            print json_encode($response);
        }
        break;
}

mysql_close($con);


